package com.sinovate.liteEMS.dao;

import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Repository("jedisDS")
public class JedisDataSourceImpl implements IDataSource {

	@Autowired
	private ShardedJedisPool shardedJedisPool;

	@Override
	public ShardedJedis getRedisClient() {
		ShardedJedis shardJedis = null;
		try {
			shardJedis = shardedJedisPool.getResource();
			return shardJedis;
		} catch (Exception e) {
			Logger.getGlobal().info(
					"[JedisDS] getRedisClent error:" + e.getMessage());
			if (null != shardJedis)
				shardJedis.close();
		}
		return null;
	}

	@Override
	public void returnResource(ShardedJedis shardedJedis) {
		shardedJedis.close();
	}

	@Override
	public void returnResource(ShardedJedis shardedJedis, Boolean broken) {
		shardedJedis.close();
	}
}
